package com.xxadmin.common.util.page;

import com.github.pagehelper.Page;
import com.xxadmin.common.dto.PageInfoDTO;
import com.xxadmin.common.exception.BusinessException;
import com.xxadmin.common.util.clone.CopyUtils;
import com.xxadmin.common.vo.PageInfoVO;

/**
 * 分页处理工具类
 *
 * @author:chenming
 * @date:2018/8/22
 */
public class PageUtils<T extends Object>{



    /**
     * 根据 PageInfoDTO 获取 PageInfoVO
     *
     * @author chenming
     * @date 2017-06-30
     */
    public static PageInfoVO getPageInfoVO(PageInfoDTO page, Class targetClass){
        PageInfoVO pageBeanVO = null;
        try {
            pageBeanVO = CopyUtils.copyBean(page, PageInfoVO.class);

            pageBeanVO.setResult(CopyUtils.copyList(page.getResult(), targetClass));
        } catch (Exception e) {
            throw new BusinessException("PageInfoDTO 转换为 PageInfoVO 失败",e);
        }

        return pageBeanVO;
    }

    /**
     * 根据 pageInfoDTO 获取 PageInfoVO
     *
     * @author chenming
     * @date 2017-07-06
     */
    public PageInfoVO getPageInfoVO(PageInfoDTO pageInfoDTO){
        PageInfoVO pageBeanVO = new PageInfoVO();

        pageBeanVO.setPageNum(pageInfoDTO.getPageNum());
        pageBeanVO.setPageSize(pageInfoDTO.getPageSize());
        pageBeanVO.setResult(pageInfoDTO.getResult());
        pageBeanVO.setTotal(pageInfoDTO.getTotal());
        pageBeanVO.setPages(pageInfoDTO.getPages());

        return pageBeanVO;
    }

    /**
     *
     * 根据 Page 获取 PageInfoDTO
     *
     * @param page
     * @param targetClass
     * @return
     */
    public static PageInfoDTO getPageInfoDTO(Page page, Class targetClass){
        PageInfoDTO pageInfo = null;
        try {
            pageInfo = CopyUtils.copyBean(page, PageInfoDTO.class);

            pageInfo.setResult(CopyUtils.copyList(page.getResult(), targetClass));
        } catch (Exception e) {
            throw new BusinessException("Page 转换为 PageInfoDTO 失败",e);
        }

        return pageInfo;
    }

}
